package br.com.lumis.ims.service.cloud.dataprovider;

import java.util.Iterator;
import java.util.List;

import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.table.TableDataProvider;
import lumis.doui.table.TableSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.util.ITransaction;
import br.com.lumis.ims.model.Instancia;
import br.com.lumis.ims.service.EC2Service;
import br.com.lumis.ims.service.cloud.util.ImsRequestParameters;

import com.amazonaws.auth.AWSCredentials;

public class InstanciaDataProvider  extends TableDataProvider {

	public InstanciaDataProvider() throws PortalException {
		super();		
	}
	
	@Override
	public void loadData(SessionConfig sessionConfig, TableSource source,
			ITransaction transaction) throws PortalException {
		
		AWSCredentials credentials = ImsRequestParameters.getCredentials(sessionConfig, transaction);
		TabularData tabularData = source.getData();
					
		List<Instancia> instancias = new EC2Service().getServidores(credentials);
		Iterator<Instancia> itInstancias = instancias.iterator();
		while(itInstancias.hasNext()) {						
			ISourceData sourceData = tabularData.addRow();
			
			Instancia instancia = itInstancias.next();
			sourceData.put(ImsRequestParameters.ID, instancia.getId());
			sourceData.put(ImsRequestParameters.NOME, instancia.getNome());
			sourceData.put(ImsRequestParameters.DESCRICAO, instancia.getDescricao());			
		}				
	}

	
}
